i =1
while i<=3:
    i += 1
    username = input("请输入用户名:")
    passwd = input("请输入密码:")
    if username == "root" and passwd == "123456":
        print("登录成功")
        goods = {'F00001': {'name': '苹果', 'price': 16.5}, 'F00002': {'name': '香蕉', 'price': 11.0}, }
        
        str = "<商品列表>"
        print(str.center(42, "-"))
        print('F00001', goods['F00001']['name'], goods['F00001']['price'])
        print('F00002', goods['F00002']['name'], goods['F00002']['price'])  # 显示商品的信息
        budget = float(input('请输入预算(元):\n'))
        residue = budget  # 定义剩余的预算为全局变量
        nowgoods = []  # 定义一个列表为当前商品列表


        def buygoods1():  # 一次购买的价格函数
            global price1

            str1 = input('请输入商品编号:\n')

            x = float(input(f'请输入想要购买的重量(kg):\n'))

            if str1 == 'F00001':
                price1 = x * float(goods['F00001']['price'])
                nowgoods.append(f'苹果  {x}kg 本次所需金额为{price1}￥')
            elif str1 == 'F00002':
                price1 = x * float(goods['F00002']['price'])
                nowgoods.append(f'香蕉  {x}kg 本次所需金额为{price1}￥')
            print(f'本次所需要的金额为{price1}')


        while 1:
            totalprice = budget - residue  # 设立一个总金额
            str2 = input('请输入exit、购买、打印当前商品与剩余金额与总金额\n')
            if str2 == 'exit':
                print(nowgoods)
                print(f'剩余预算为{residue}')
                print(f'本次购物所需总金额为{totalprice}')
                break
            elif str2 == '购买':
                buygoods1()
                print(f'当前剩余预算为{residue}')
                residue -= price1  # 剩余的预算
                print(f'购买后剩余预算为{residue}')
                if residue <= 0:  # 当当前预算小于0时就退出购买
                    print('预算已超出不可购买当前商品并打印之前商品')
                    nowgoods.pop()
                    print(nowgoods)
                    print(f'本次购物所需总金额为{totalprice}')
                    break



            elif str2 == '打印当前商品与剩余金额与总金额':
                print(nowgoods)
                print(f'剩余预算为{residue}')
                print(f'本次购物所需总金额为{totalprice}')





        break
    else:
        print(f"用户名或者密码错误，还剩{4-i}次机会")
else:
    print("重试超过3次，用户登录失败")
#
